<?php

namespace frontend\models;

use common\helps\tools;
use Yii;
use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;
use yii\db\Exception;
use yii\db\Query;

/**
 * This is the model class for table "ProjectActor".
 *
 */
class ProjectActor extends ActiveRecord
{

    public $type;
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%project_actor}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return[
            [['name','position','role','avatar'], 'trim'],
            [['name','position','avatar'], 'required', 'message'=>'{attribute}不能为空'],
            [['id','type'], 'match', 'pattern' => '/^[1-9][0-9]*$/','message'=>'{attribute}值无效'],
        ];
    }


    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => '演员id',
            'name' => '姓名',
            'position'=>'职业',
            'role'=>'角色',
            'project_id'=>'项目id',
            'avatar'=>'角色头像',
            'type'=>'操作类型',
        ];
    }


    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            TimestampBehavior::className(),
        ];
    }

    public function scenarios()
    {
        return [
            'default' => [
                'name',
                'position',
                'role',
                'avatar',
                'created_at',
                'updated_at',
                'deleted_at',
                'type',
            ],
            'create' => [
                'name',
                'position',
                'role',
                'avatar',
                'project_id',
                'created_at',
                'updated_at',
                'deleted_at',
            ],
            'update'=>[
                'id',
                'name',
                'position',
                'role',
                'avatar',
                'created_at',
                'updated_at',
                'deleted_at',
            ],
            'delete'=>[
                'id',
                'created_at',
                'updated_at',
                'deleted_at',
            ],
        ];
    }

    /**
     * 获取演职人员列表
     * @param $params
     * @author hey_heyang(635870453@qq.com)
     * @return array
     */
    public function getProjectActorList($params){
        $list = $this->find()->select('*,FROM_UNIXTIME(created_at) as create_time')->asArray()->where(['deleted_at'=>null,'project_id'=>intval($params['project_id'])])->all();
        return empty($list)?array():$list;

    }

    /**
     * 获取演职人员信息
     * @param $params
     * @author hey_heyang(635870453@qq.com)
     * @return array
     */
    public function getProjectActorInfo($params){
        if(!empty($params['name'])){
            $list = $this->find()->select('*,FROM_UNIXTIME(created_at) as create_time')->asArray()->where(['and',['deleted_at'=>null],['name'=>trim($params['name'])]])->all();
            return empty($list)?array():$list;
        }else{
            return [];
        }


    }



}
